package io.gitee.hechenl.fastiotreport.controller;

import cn.hutool.core.date.DateUtil;
import io.gitee.hechenl.fastiotreport.controller.converter.MetricConverter;
import io.gitee.hechenl.fastiotreport.domain.dto.MetricListDTO;
import io.gitee.hechenl.fastiotreport.domain.model.ConfigMetric;
import io.gitee.hechenl.fastiotreport.domain.model.ConfigMetricGroup;
import io.gitee.hechenl.fastiotreport.domain.vo.MetricVO;
import io.gitee.hechenl.fastiotreport.service.IConfigMetricGroupService;
import io.gitee.hechenl.fastiotreport.service.IMetricService;
import io.gitee.hechenl.fastiotreport.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RequestMapping("/metric")
@RestController
public class MetricController {

    @Autowired
    private IMetricService metricService;
    @Autowired
    private IConfigMetricGroupService configMetricGroupService;

    @GetMapping("/list")
    public Result<MetricVO> list(@Valid MetricListDTO dto) {
        List<ConfigMetricGroup> allGroupList = configMetricGroupService.list();
        Map<ConfigMetric, BigDecimal> metricMap = metricService.values(
                dto.getMetricIds(),
                DateUtil.parseLocalDateTime(dto.getStartTime()),
                DateUtil.parseLocalDateTime(dto.getEndTime()));
        return Result.success(MetricConverter.toVO(metricMap, allGroupList));
    }
}
